/**
 * 
 */
package com.csair.youth.web.interceptor;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.csair.youth.constant.Constant;
import com.csair.youth.database.Permission;
import com.csair.youth.database.SysConfig;
import com.csair.youth.service.impl.SysConfigServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * @author mickey
 *
 */
public class RepoStatusInterceptor extends AbstractInterceptor {
	private boolean enable = false;
	private ApplicationContext appContext;
	private SysConfigServiceImpl sysConfigService;
	
	private static final Logger log = Logger.getLogger(RepoStatusInterceptor.class);
	
	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}


	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
	 */
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext ctx = invocation.getInvocationContext();
		Map session = ctx.getSession();
		if(session.get(Constant.USER_ID_SESSION_KEY) == null){
			//did not logon, skip
			return invocation.invoke();
		}
		if(sysConfigService == null){
			ServletContext servletContext = (ServletContext)ctx.get(ServletActionContext.SERVLET_CONTEXT);
			appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
			sysConfigService = (SysConfigServiceImpl)appContext.getBean("sysConfigService");
		}

		if(!isEnable()){
			return invocation.invoke();
		}
		
		SysConfig config = sysConfigService.getSysConfigByKey(Constant.REPO_STATUS_KEY);
		if(config == null || !Constant.REPO_STATUS_OPEN.equals(config.getConfigValue())){
			//close...
			if(session.get(Constant.USER_PERMISSION_MAP_KEY) != null){
				List permissions = (List)session.get(Constant.USER_PERMISSION_MAP_KEY);
				if(permissions != null && permissions.size() > 0){
					boolean isAdmin = false;
					Iterator iter = permissions.iterator();
					while(iter.hasNext()){
						Permission p = (Permission)iter.next();
						if("/SysConfigList.action".equals(p.getUrl())){
							//OK
							isAdmin = true;
							break;
						}
					}
					if(!isAdmin){
						clearSession(session);
						return "login";
					}
					else return invocation.invoke();
				}
				else{
					clearSession(session);
					return "login";
				}
			}
			else{
				clearSession(session);
				return "login";
			}
		}
		else return invocation.invoke();
	}
	
	private void clearSession(Map session){
		if(session.get(Constant.USER_ID_SESSION_KEY) != null){
			session.put(Constant.USER_ID_SESSION_KEY, null);
			session.put(Constant.USER_NAME_SESSION_KEY, null);
			session.put(Constant.USER_ROLE_SESSION_KEY, null);
			session.put(Constant.USER_PERMISSION_MAP_KEY, null);
		}
	}
}
